package com.dlc.felear.xiaoensale.helper;


import android.util.Log;
import com.dlc.felear.xiaoensale.utils.MyLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;
import java.io.File;
import java.util.Map;
import okhttp3.MediaType;

/**
 * @author Created by admin on 2018/5/6.
 */

public class OkHttpManager {

    private static String TAG="xiefkm";
    /**
     * get请求
     *
     * @param url
     * @param params
     * @param callback
     */
    public static void HttpGet(String url, Map<String, String> params, Callback callback) {
        OkHttpUtils
                .get()
                .url(url)
                .params(params)
                .build()
                .execute(callback);
    }
    /**
     * get请求
     *
     * @param url
     * @param callback
     */
    public static void HttpGet(String url, Callback callback) {
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);
    }

    /**
     * Post请求
     *
     * @param url
     * @param params
     * @param callback
     */
    public static void HttpPost(String url, Map<String, String> params, Callback callback) {
        OkHttpUtils
                .post()
                .url(url)
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * PostJson请求
     *
     * @param url
     * @param callback
     */
    public static void HttpPostJson(String url, Map<String, Object> json, Callback callback) {
        String content=JsonHelper.toJson(json);
        Log.e("OkHttpManager",content);
        OkHttpUtils
                .postString()
                .url(url)
                .content(content)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(callback);
    }

    /**
     * PostJson请求
     *
     * @param url
     * @param callback
     */
    public static void HttpPostJson(String url, String content,String tokenValue, Callback callback) {
        OkHttpUtils
                .postString()
                .url(url)
                .content(content)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(callback);
    }




    /**
     * 表单形式上传文件
     *
     * @param url
     * @param params
     * @param callback
     */
    public static void HttpPostFile(String url, Map<String, String> params, Map<String, String> headers
            , File file, Callback callback) {
        OkHttpUtils.post()
                .addFile("file", file.getName(), file)
                .url(url)
                .params(params)
                .headers(headers)
                .build()
                .execute(callback);
    }

    /**
     * 下载文件
     * @param url
     * @param callback
     */
    public static void HttpDownFile(String url, FileCallBack callback) {
        MyLog.e("qid",url);
        OkHttpUtils
                .get()
                .url(url)
                .build()
                .execute(callback);
    }
}
